<template>
	<div class="app-container" v-loading="listLoading">
    <div class="filter-container">
      <div class="fl width300" v-if="permission == 3">
        <el-button type="primary" class="filter-item" icon="el-icon-edit" @click="linkToCreate">新增讲师</el-button>
      </div>
      <div class="fr">
        <el-input @keyup.enter.native="handleFilter" class="filter-item" v-model="listQuery.name" clearable
                  placeholder="请输入讲师名称进行搜索" style="width: 250px;"></el-input>
        <el-button class='search filter-item' type="primary"  icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </div>
    </div>

    <el-table :data="tableData" :header-row-style="{'font-family': 'PingFangSC-Semibold','backgroundColor': '#f5f7fa','color':'#5c6065'}" element-loading-text="加载中..." fit highlight-current-row style="width: 100%;font-size: 12px;color:#222;font-family: PingFangSC-Regular;">
      <el-table-column width="80" align="center" label="编号">
        <template slot-scope="scope">
          <span>{{ scope.row.lecturerId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="讲师头像">
        <template slot-scope="scope">
          <span><img :src="scope.row.avatar" style="width:80px;"></span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="讲师名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
       <el-table-column align="center" label="讲师分成比例">
        <template slot-scope="scope">
          <span>{{ scope.row.ratio }}%</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="讲师身份">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" v-if="permission == 3">
        <template slot-scope="scope">
          <b @click="linkToEdit(scope.row.lecturerId)">编辑</b>
          <span class="color-409">|</span>
          <b @click="handleDelete(scope.$index, scope.row.lecturerId)">删除</b>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  import {getLecturerList, deleteLecturer} from '@/api/lecturer'
  import {shopManage_lecturers} from '@/utils/permission'
  export default {
    data() {
      return {
        permission: 3,
        listQuery: {
          name: null,
          page: 1,
          limit: 20
        },
        tableData: [],
        total: null,
        listLoading: false
      }
    },
    created() {
      this.getModelPermission()
      this.initList()
    },
    methods: {
      getModelPermission() {
        this.permission = shopManage_lecturers()
      },
      initList() {
        this.listLoading = true
        getLecturerList(this.listQuery).then(res => {
          this.tableData = res.data.data.data
          this.total = res.data.data.total
          this.listLoading = false
        }).catch(err => {
          this.listLoading = false
          console.log(err)
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.initList()
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.initList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.initList()
      },
      handleDelete(index, id) {
        this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.listLoading = true
          deleteLecturer(id).then(res => {
          	this.listLoading = false
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.tableData.splice(index, 1)
          }).catch(err => {
          	this.listLoading = false
            console.log(err)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      linkToCreate() {
        this.$router.push({path: '/lecturers/create'})
      },
      linkToEdit(id) {
        this.$router.push({path: `/lecturers/${id}/edit`})
      }
    }
  }
</script>
